cmake_minimum_required(VERSION 3.12)  # 设置CMake最低版本要求
project(app)                          # 项目名称（对应TARGET）

set(CMAKE_CXX_STANDARD 17)            # -std=c++17
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
    # GCC或Clang编译器
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic")
elseif(MSVC)
    # MSVC编译器
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /permissive-")
endif()

# 平台特定设置
if(WIN32)
    message(STATUS "Configuring for Windows platform")
    set(GLFW_ROOT "D:/software/glfw")
    # 查找GLFW库
    if(MINGW)
        message(STATUS "MINGW Build env")
        
        # MinGW 编译器设置
        set(CMAKE_C_COMPILER "gcc")
        set(CMAKE_CXX_COMPILER "g++")
        
        # MinGW 库路径
        set(GLFW_LIB_DIR "${GLFW_ROOT}/lib-mingw-w64")
        set(GLFW_LIB_NAME "glfw3") # MinGW通常使用短名称
    elseif(MSVC)
        message(STATUS "MSVC Build env")
        # MSVC版本检查
        if(MSVC_VERSION LESS 1800)
            message(FATAL_ERROR "Unsupported Visual Studio version (too old)")
        elseif(MSVC_VERSION LESS 1900)
            set(GLFW_LIB_DIR "${GLFW_ROOT}/lib-vc2013")
        elseif(MSVC_VERSION LESS 1910)
            set(GLFW_LIB_DIR "${GLFW_ROOT}/lib-vc2015")
        elseif(MSVC_VERSION LESS 1920)
            set(GLFW_LIB_DIR "${GLFW_ROOT}/lib-vc2017")
        elseif(MSVC_VERSION LESS 1930)
            set(GLFW_LIB_DIR "${GLFW_ROOT}/lib-vc2019")
        else()
            set(GLFW_LIB_DIR "${GLFW_ROOT}/lib-vc2022")
        endif()
        set(GLFW_LIB_NAME "glfw3.lib")   # MSVC需要.lib后缀
        
    else()
        message(FATAL_ERROR "Unsupported compiler")
    endif()

   
elseif(APPLE)
    # macOS平台设置
    message(STATUS "Configuring for macOS platform")
else()
    message(FATAL_ERROR "Unsupported platform")
endif()



set(ENGINE_SOURCES
    editor/editor.cpp
    editor/layout/layout-asset.cpp
    editor/layout/layout-config.cpp
    editor/layout/layout-hierarchy.cpp
    editor/layout/layout-menu.cpp
    editor/layout/layout-property.cpp
    editor/layout/layout-scene.cpp
    editor/layout/layout-tool.cpp
    engine/core/asset/asset-mgr.cpp
    engine/core/component/component.cpp
    engine/core/font/freetype-mgr.cpp
    engine/core/gfx/gfx-mgr.cpp
    engine/core/gfx/gfx-object.cpp
    engine/core/gfx/gfx-pass.cpp
    engine/core/gfx/gfx-pipeline.cpp
    engine/core/gfx/gfx-shader.cpp
    engine/core/gfx/gfx-texture.cpp
    engine/core/global/event.cpp
    engine/core/global/renderer.cpp
    engine/core/global/view.cpp
    engine/core/math/color.cpp
    engine/core/math/mat4.cpp
    engine/core/math/quat.cpp
    engine/core/math/size.cpp
    engine/core/math/vec2.cpp
    engine/core/math/vec3.cpp
    engine/core/math/vec4.cpp
    engine/core/renderer/node.cpp
    engine/core/renderer/scene.cpp
    engine/core/renderer/ui/label.cpp
    engine/core/renderer/ui/sprite.cpp
    engine/core/renderer/ui/ui-renderer.cpp
    engine/core/utils/sha.cpp
    engine/core/utils/time.cpp
    engine/core/window/window-mgr.cpp
    engine/engine.cpp
)

add_executable(
    ${PROJECT_NAME} 
    ${ENGINE_SOURCES}
    app.cpp
)  

# 包含 STB 头文件目录
target_include_directories(${PROJECT_NAME} PRIVATE 
    ${CMAKE_CURRENT_SOURCE_DIR}/engine/libs/stb
)

if(WIN32)
    # 查找Vulkan库
    find_package(Vulkan REQUIRED)
    #  target_include_directories(${PROJECT_NAME} PRIVATE "${GLFW_ROOT}/include")
    # target_link_directories(${PROJECT_NAME} PRIVATE "${GLFW_LIB_DIR}")
    include_directories("C:/msys64/mingw64/include/freetype2") # 包含freetype头文件目录
    # find_library(Freetype freetype PATHS "C:/msys64/mingw64/lib") # 根据你的环境替换

    target_link_libraries(${PROJECT_NAME} PRIVATE
        glfw3
        Vulkan::Vulkan
        Freetype
    )
elseif(APPLE)
      # 查找GLFW库
      find_package(glfw3 REQUIRED)
    # 查找Vulkan库
    find_package(Vulkan REQUIRED)
    # 查找freetype库
    find_package(Freetype REQUIRED)

    target_link_libraries(${PROJECT_NAME} PRIVATE
        glfw
        Vulkan::Vulkan
        Freetype::Freetype
    )
else()
    message(FATAL_ERROR "Unsupported platform")
endif()

# 文件拷贝
file(COPY engine/resources/ DESTINATION resources)